package com.gdepri.cabelwellserver.handler;

import com.alibaba.fastjson2.JSONObject;
import com.github.tocrhz.mqtt.publisher.MqttPublisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MQTTHandler {

    @Autowired
    MqttPublisher mqttPublisher;

    private final String prefix = "T651GW202305280006000002_cw";
    private Logger logger = LoggerFactory.getLogger(MQTTHandler.class);

    public void topoAdd(){
        String gatewayId = "123456789012345678901234";
        JSONObject msg = new JSONObject();
        msg.put("测试","ok");
        msg.put("number",24);
        String topic = String.format("/v5/devices/%s/datas", gatewayId);
        mqttPublisher.send(String.format("/v5/devices/%s/datas", gatewayId), msg);
        System.out.println("发出消息，主题为：{"+topic+"}，请求体为：{"+msg+"}");
    }

    public void sendData(){

    }

    public String getDeviceId(int address){
        return prefix+address;
    }

    //
    public void sendData(JSONObject resultObj, int address) {
         String deviceId = getDeviceId(address);
         //给物管平台发送消息，数据是resultObj,deviceId形如T651GW202305280006000002_cw1
        logger.info("MQTT处理程序，处理设备数据："+deviceId);
        logger.info(resultObj.toJSONString());
    }
}
